package com.problem.leetcode;

/**
 * @author didi
 * @DESCRIPTION
 * @create 2019-06-04 13:52
 **/

public class Problem704 {
    public int search(int[] nums, int target) {
        return search(nums, 0, (nums.length - 1), target);
    }

    private int search(int[] nums, int begin, int end, int target) {
        if (begin == end) {
            if (nums[begin] == target) {
                return begin;
            }
        }else if (begin > end) {
            return -1;
        }

        int middle = (begin + end) >> 1;
        if (nums[middle] < target) {
            return search(nums, middle + 1, end, target);
        } else if (nums[middle] > target) {
            return search(nums, begin, middle - 1, target);
        } else {
            if (nums[middle] == target) {
                return middle;
            }
        }
        return -1;
    }
}
